package com.stargazer.ming.ui.util;

import java.io.File;

import com.stargazer.ming.R;
import com.stargazer.ming.storage.ConstantsStorage;
import com.stargazer.ming.ui.MainUI;
import com.stargazer.ming.util.FileOperation;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.provider.MediaStore.MediaColumns;
import android.widget.Toast;

public final class CameraUtil {
	private static String filePath = null;
	
	public static boolean selectPicture(final Activity activity, final int cmd) {
		final Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
		intent.setType("image/*");
		try {
			activity.startActivityForResult(Intent.createChooser(intent, null), cmd);

		} catch (ActivityNotFoundException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	public static boolean takePhoto(final Activity activity, final String dir, final String filename,
			final int cmd) {
		filePath = dir + filename;

		final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		final File cameraDir = new File(dir);
		if (!cameraDir.exists()) {
			cameraDir.mkdirs();
			//return false;
		}

		final File file = new File(filePath);
		final Uri outputFileUri = Uri.fromFile(file);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
		try {
			activity.startActivityForResult(intent, cmd);
		} catch (final ActivityNotFoundException e) {
			return false;
		}
		return true;
	}
	
	public static String getResultPhotoPath(Context context, final Intent intent){
		if(FileOperation.fileExists(filePath)){
			return filePath;
		}
		return resolvePhotoFromIntent(context, intent);
	}
	
	public static String resolvePhotoFromIntent(final Context context, final Intent intent){
		if(context==null || intent==null){
			return null;
		}
		
		String filePath = null;
		Cursor cu=context.getContentResolver().query(intent.getData(), null, null, null, null);
		if(cu!=null && cu.moveToFirst()){
			final int pathIndex = cu.getColumnIndex(MediaColumns.DATA);
			filePath=cu.getString(pathIndex);
		}
		return filePath;
	}
	
	public static void showPhotoDialog(final Activity activity){
    	AlertDialog.Builder builder=new AlertDialog.Builder(activity);
    	builder.setTitle(R.string.title_dialog_select_cover).setItems(R.array.select_cover,new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				switch (which) {
				case 0:
					selectPicture(activity, MainUI.CONTEXT_MENU_IMAGE);
					break;
				case 1:
					if (!takePhoto(activity, ConstantsStorage.DATAROOT_SDCARD_CAMERA_PATH, "Ming." + System.currentTimeMillis() + ".jpg", MainUI.CONTEXT_MENU_TAKE_PICTURE)) {
						Toast.makeText(activity, activity.getResources().getString(R.string.select_camera_none), Toast.LENGTH_LONG).show();
					}
					break;

				default:
					break;
				}
				
			}
		});
    	builder.create().show();
    }
}
